package com.af.data.jpa.entity;

import org.springframework.data.domain.AuditorAware;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @author : zhenyun.su
 * @comment : createdBy, lastModifiedBy栏位提供操作用户，
 * 使用说明:
 * 1. 实现接口AuditorUser，并注入到 SzyAuditorAware
 * 2. 将SzyAuditorAware注册为Bean
 * 来源说明： 21-data-jpa-mysql -> com/szy/common
 * @since : 2020/1/10
 */

public class AfAuditorAware implements AuditorAware<String> {
    public static final String USERNAME = "system";

    private AuditorUser auditorUser;

    public AfAuditorAware(AuditorUser auditorUser) {
        this.auditorUser = auditorUser;
    }

    @Override
    public Optional<String> getCurrentAuditor() {
        String name = USERNAME;
        try {
            if (StringUtils.hasText(auditorUser.getUserName())) {
                name = auditorUser.getUserName();
            }
        } catch (Exception e) {
            name = USERNAME;
        }
        return Optional.of(name);
    }
}
